package com.sljy.hzk.animation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

import com.sljy.hzk.util.EventListenerManager;

public class AnimationManager {
    private static AnimationManager animation;
    public static AnimationManager Instance(){
        if (animation== null)animation = new AnimationManager();
        return animation;
    }


    public void DoScaleAction(View view, Float fx, Float tx, Float fy, Float ty, int duration){
        Animation ani = new ScaleAnimation(fx, tx, fy, ty, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ani.setDuration(duration);//动画时间
        ani.setRepeatCount(0);//动画的重复次数
        ani.setFillAfter(true);//设置为true，动画转化结束后被应用
        view.startAnimation(ani);//开始动=
    }

    public void DoScaleAction(View view, Float fx, Float tx, Float fy, Float ty, int duration, int repeat, boolean isRepetMode){
        Animation ani = new ScaleAnimation(fx, tx, fy, ty, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ani.setDuration(duration);//动画时间
        ani.setRepeatCount(repeat);//动画的重复次数
        ani.setFillAfter(true);//设置为true，动画转化结束后被应用
        ani.setRepeatMode(Animation.REVERSE);//复原
        view.startAnimation(ani);//开始动=
    }

    public void DoScaleAction(View view, Float fx, Float tx, Float fy, Float ty, int duration, Integer type){
        Animation ani = new ScaleAnimation(fx, tx, fy, ty, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        ani.setDuration(duration);//动画时间
        ani.setRepeatCount(0);//动画的重复次数
        ani.setFillAfter(true);//设置为true，动画转化结束后被应用
       // ani.setRepeatMode(Animation.REVERSE);//复原
        ani.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                try {
                    EventListenerManager.Instance().sendEvent("effctCallBack", type);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(ani);//开始动=
    }

    /**
     * 改变颜色
     * */
    public void DoColorAction(View view,@ColorInt int tc){
        view.setBackgroundColor(tc);
    }

    public void DrawOutLine(View view){
        Drawable draw = view.getBackground();
        Bitmap mContainer = Bitmap.createBitmap(draw.getIntrinsicWidth(),draw.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas mContainerCanvas=new Canvas(mContainer);
        //在container上绘制只有alpha通道的img
        Paint mBitPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBitPaint.setFilterBitmap(true);
        mBitPaint.setDither(true);
        Rect mSrcRect, mDestRect;
        mSrcRect = new Rect(0, 0, mContainer.getWidth()/2, mContainer.getHeight()/2);
        mDestRect = new Rect(0, 0, mContainer.getWidth()/2, mContainer.getHeight()/2);
        mContainerCanvas.drawBitmap(mContainer,mSrcRect ,mDestRect,mBitPaint);
    }

}
